package cn.neusoft.servlet;


import cn.neusoft.bean.DeliveryAddress;
import cn.neusoft.query.DeliveryAddressQuery;
import cn.neusoft.service.IDeliveryAddressService;
import cn.neusoft.service.impl.DeliveryAddressService;
import cn.neusoft.utils.IOUtils;
import cn.neusoft.utils.JsonUtils;
import cn.neusoft.vo.ResultBean;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/address")
public class DeliveryAddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------DeliveryAddressServlet------");
        IDeliveryAddressService deliveryAddressService = new DeliveryAddressService();
        String what = req.getParameter("what");

        //最终的响应体对象
        ResultBean resultBean = null;

        try {
            if("save".equals(what)){
                System.out.println("添加或修改地址信息");
                String jsonStr = IOUtils.readStr(req.getInputStream());
                DeliveryAddress deliveryAddress = JsonUtils.jsonStrToObj(jsonStr, "yyyy-MM-dd",DeliveryAddress.class);
                if (deliveryAddress.getDaId()==null){
                    resultBean = deliveryAddressService.add(deliveryAddress);
                }else {
                    resultBean = deliveryAddressService.update(deliveryAddress);
                }
            }else if("del".equals(what)){
                System.out.println("删除地址信息");
                String id = req.getParameter("id");
                resultBean = deliveryAddressService.delete(Integer.parseInt(id));
            }else if("getById".equals(what)){
                System.out.println("通过id查询地址信息");
                String id = req.getParameter("id");
                resultBean = deliveryAddressService.getById(Integer.parseInt(id));
            }else if("page".equals(what)){
                System.out.println("分页搜索地址信息");
                String jsonStr = IOUtils.readStr(req.getInputStream());
                DeliveryAddressQuery deliveryAddressQuery = JsonUtils.jsonStrToObj(jsonStr, null, DeliveryAddressQuery.class);

                resultBean = deliveryAddressService.getByPage(deliveryAddressQuery);
            }else{
                System.out.println("无服务");
                resultBean = ResultBean.ERROR("no service");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultBean = ResultBean.ERROR(e.getMessage());
        }

        //将resultBean转成json字符串响应即可
        String resultJsonStr = JSONObject.toJSONString(resultBean);
        resp.getWriter().write(resultJsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
